var _processFuncTree;

Ext.onReady(function() {
    new Ext.Viewport({
        layout:'border',
        items:[
            new Ext.BoxComponent({
                region:'north',
                el: 'north',
                height:60
            }),
            new Ext.BoxComponent({
                region:'south',
                el: 'south',
                height:25
            }),
            {
                region:'west',
                id:'west-panel',
                split:true,
                width: 200,
                minSize: 175,
                maxSize: 400,
                margins:'0 0 0 0',
                layout:'accordion',
                title:' ',
                collapsible :true,
                layoutConfig:{ animate:true },
                items: [{
                    title:'客户信息管理',
                    html:'<div id="customerFuncTree" style="overflow:auto;width:100%;height:100%"></div>'
                }, {
                    title:'联系人信息管理',
                    html:'<div id="contactFuncTree" style="overflow:auto;width:100%;height:100%"></div>'
                }, {
                    title:'业务进展管理',
                    html:'<div id="processFuncTree" style="overflow:auto;width:100%;height:100%"></div>'
                }, {
                    title:'系统设置',
                    html:'<div id="systemFuncTree" style="overflow:auto;width:100%;height:100%"></div>'
                }]
            },
            mainPanel

        ]
    });


    // Customer Management
    var customerManage = new Ext.tree.TreeNode({
        id:'customerManage',
        text: '客户管理'
    });

    var customer1 = new Ext.tree.TreeNode({
        id:'CustomerAll',
        text:'客户基本信息维护'
    })

    var currentWeek = new Ext.tree.TreeNode({
        id:'CurrentWeek',
        text:'本周客户'
    })

    var lastWeek = new Ext.tree.TreeNode({
        id:'LastWeek',
        text:'上周客户'
    })

    customerManage.appendChild(customer1);
    customerManage.appendChild(currentWeek);
    customerManage.appendChild(lastWeek);

    var customerFuncTree = new Ext.tree.TreePanel({
        el:'customerFuncTree',
        autoScroll:true,
        root:customerManage,
        animate:true,
        enableDD:false,
        border:false,
        rootVisible:false,
        containerScroll: true
    });

    customerFuncTree.render();
    customerFuncTree.on('click', treeClick);


    // Contact Management
    var contactManage = new Ext.tree.TreeNode({
        id:"contactManage",
        text:"联系人管理"
    });

    var contactInfo = new Ext.tree.TreeNode({
        id:'ContactOfCustomer',
        icon:'images/dfr/im2.gif',
        text:'联系人信息维护'
    });

    var contactSearch = new Ext.tree.TreeNode({
        id:'ContactSearch',
        icon:'images/dfr/im2.gif',
        text:'联系人检索'
    });

    contactManage.appendChild(contactInfo);
    contactManage.appendChild(contactSearch);

    var contactFuncTree = new Ext.tree.TreePanel({
        renderTo:"contactFuncTree",
        root:contactManage,
        animate:true,
        enableDD:false,
        border:false,
        rootVisible:false,
        containerScroll: true
    });

    contactFuncTree.on('click', treeClick);


    // Process Management
    var processManage = new Ext.tree.TreeNode({
        id:"processManage",
        text:"业务进展管理"
    });

    var processRecord = new Ext.tree.TreeNode({
        id:'CustomerSelect',
        text:'沟通记录维护'
    })

    var processHistory = new Ext.tree.TreeNode({
        id:'ProcessHistory',
        text:'历史沟通'
    })

    processManage.appendChild(processRecord);
    processManage.appendChild(processHistory);

    _processFuncTree = new Ext.tree.TreePanel({
        renderTo:"processFuncTree",
        root:processManage,
        animate:true,
        enableDD:false,
        border:false,
        rootVisible:false,
        containerScroll: true      
    });

    _processFuncTree.on('click', treeClick);
    _processFuncTree.expandAll();

    // System Management
    var systemManage = new Ext.tree.TreeNode({
        id:"systemManage",
        text:"系统设置"
    });

    var changePwd = new Ext.tree.TreeNode({
        id:'SysModifyPassword',
        text:'修改密码'
    });

    var sysUser = new Ext.tree.TreeNode({
        id:'SysUser',
        text:'用户管理'
    })

    var sysOptRecord = new Ext.tree.TreeNode({
        id:'SysOptRecord',
        text:'操作统计'
    })

    var sysListbox = new Ext.tree.TreeNode({
        id:'SysListbox',
        text:'列表维护'
    })

    systemManage.appendChild(changePwd);
    if (suFlag == "true") {
        systemManage.appendChild(sysUser);
        systemManage.appendChild(sysOptRecord);
        systemManage.appendChild(sysListbox);
    }

    var systemFuncTree = new Ext.tree.TreePanel({
        renderTo:"systemFuncTree",
        root:systemManage,
        animate:true,
        enableDD:false,
        border:false,
        rootVisible:false,
        containerScroll: true
    });

    systemFuncTree.on('click', treeClick);

    setTimeout(endLoading, 250);
});

/** 标识是否超级用户 */
var suFlag = "false";

/**
 * 主面板，进入系统时默认加载提醒信息页。
 */
var mainPanel = new Ext.Panel({
    region:'center',
    id: 'mainPanel',
    title: '我的提醒',
    autoScroll:true,
    html: '<iframe id="mainFrame" scrolling="auto" frameborder="0" width="100%" height="100%" src="../app?page=UserRemind&service=page"></iframe>'
});

function endLoading() {
    Ext.get('loading').remove();
    Ext.get('loading-mask').fadeOut({remove:true});
}

/**
 * 树的点击事件
 */
function treeClick(node, e) {
    switchMainPanel(node.id, node.text);
}

/**
 * 切换主面板显示
 */
function switchMainPanel(id, title) {
    document.getElementById("mainFrame").src = "../app?page=" + id + "&service=page";
    mainPanel.setTitle(title);
}

function showProcessMenuPanel() {
}

